<script lang="ts" setup name="XtxCity">
import { ref } from 'vue';
import {onClickOutside} from '@vueuse/core'
import axios from 'axios'
import { watch } from 'vue';


export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}

// 接收父组件传的值
const {userAddress=''} = defineProps<{
  userAddress?:string
}>()

const emit = defineEmits<{
  (e:'changeCity',value:CityResult):void
}>()


//声明初始状态
const flag = ref(false)
const target = ref(null)
//切换效果
const toggle = ()=>{
  flag.value = !flag.value
}
//点击外部关闭弹层
onClickOutside(target,()=>{
  //点击外部,该回调函数才执行
  flag.value = false
})


// 在 *组件内部* 获取省市区数据 
// 1.一次获取所有的数据(省市区) *
// 2.先获取省,点击省获取市,点击市获取区
// 3.用常量保存省市区数据
type AreaList = {
   code:string
   level:number
   name:string
   areaList:AreaList[]
}

const cityList = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])  //缓存所有数据
const getCityList = async ()=>{
  const res = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  cityList.value = res.data
  cacheList.value = res.data
}
getCityList()


// 数据交互
// 选择城市
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})

const selectCity =(item:AreaList)=>{
  if(item.level===0){  //选择省
     changeResult.value.provinceName = item.name
     changeResult.value.provinceCode = item.code
     cityList.value = item.areaList
  }
   if(item.level===1){  //选择市
     changeResult.value.cityName = item.name
     changeResult.value.cityCode = item.code
     cityList.value = item.areaList
  }
   if(item.level===2){  //选择区
     changeResult.value.countyName = item.name
     changeResult.value.countyCode = item.code
     flag.value = false  //关闭弹层

     emit('changeCity',changeResult.value)
  }
}


// 侦听器侦听flag的变化  当flag===false代表关闭 就重置数据
watch(flag,()=>{
  if(!flag.value){
    cityList.value = cacheList.value
  }
})
</script>

<template>
  <div class="xtx-city" ref="target">

    <div class="select" :class="{active:flag}" @click="toggle">
      <span class="value" v-if="userAddress">{{userAddress}}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>

    <!-- 城市选择弹层 -->
    <div class="option" v-show="flag">
      <span 
        class="ellipsis" 
        v-for="item in cityList" 
        :key="item.code"
        @click="selectCity(item)"
      >
        {{item.name}}
      </span>
    </div>

  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
